package com.easemob.easeui.utils;

import android.content.Context;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudcc.cloudframe.util.LogUtils;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.CoworkerEntity;
import com.cloudcc.mobile.im.Constant;
import com.cloudcc.mobile.manager.ContactsManager;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.util.UrlTools;
import com.cloudcc.mobile.util.UserUtils;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMConversation;
import com.easemob.chat.EMMessage;
import com.easemob.easeui.controller.EaseUI;
import com.easemob.easeui.controller.EaseUI.EaseUserProfileProvider;

public class EaseUserUtils {

	static EaseUserProfileProvider userProvider;

	static {
		userProvider = EaseUI.getInstance().getUserProfileProvider();
	}

	/**
	 * 根据username获取相应user
	 * 
	 * @param username
	 * @return
	 */
	public static CoworkerEntity getUserInfo(String username) {
		if (userProvider != null) {
			CoworkerEntity user = userProvider.getUser(username);
			return user;
		}
		return null;
	}

	/** 通过环信的用户名 来获取 cloudcc 帐号系统的用户逇userId
	 * @param username
	 * @return
	 */
	public static String getCCUserIdByHXuserName(String username){
		if (UserUtils.isMe(ContactsManager.getInstance().deCodeHXuserName(username))) {//自己不能获得
			return UserManager.getManager().getUser().userId;
		}
		EMConversation conversation = EMChatManager.getInstance().getConversation(username);
		if (conversation==null) {
			return null;
		}
		EMMessage message = conversation.getLastMessage();
		if (message==null||message.getChatType()!=EMMessage.ChatType.Chat) {
			return ContactsManager.getInstance().deCodeHXuserName(username);
		}
		//获得用户的记录
		String userId ="";
		if (message.direct == EMMessage.Direct.RECEIVE) {// 是接收的消息 获得发送的人名
			userId = message.getStringAttribute(Constant.MESSAGE_ATTR_MESSAGE_SEND_ID, userId);
		} else {// 是发送的然后 获取接受者的
			userId = message.getStringAttribute(Constant.MESSAGE_ATTR_MESSAGE_RECEIVE_ID,userId);
		}
		return userId;
	}
	/** 根据 环信的用户名 获得 用户的显示名称
	 * @param username
	 * @param defaultNickName
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String getCCUserNickNameByHXuserName(String username,String defaultNickName){
		if (UserUtils.isMe(ContactsManager.getInstance().deCodeHXuserName(username))) {//自己不能获得
			return UserManager.getManager().getUser().userName;
		}
		EMConversation conversation = EMChatManager.getInstance().getConversation(username);
		if (conversation==null) {
			LogUtils.d("imchat", "从环信用户名中获得昵称(会话空)"+defaultNickName);
			return defaultNickName;
		}
		EMMessage message = conversation.getLastMessage();
		if (message==null||message.getChatType()!=EMMessage.ChatType.Chat) {
			LogUtils.d("imchat", "从环信用户名中获得昵称(最后消息空)"+defaultNickName);
			return defaultNickName;
		}
		//获得用户的记录
		String userName;
		if (message.direct == EMMessage.Direct.RECEIVE) {// 是接收的消息 获得发送的人名
			userName = message.getStringAttribute(Constant.MESSAGE_ATTR_MESSAGE_SEND_NICK, defaultNickName);
			LogUtils.d("imchat", "从环信用户名中获得昵称(接收)"+userName);
		} else {// 是发送的然后 获取接受者的
			userName = message.getStringAttribute(Constant.MESSAGE_ATTR_MESSAGE_RECEIVE_NICK,defaultNickName);
			LogUtils.d("imchat", "从环信用户名中获得昵称(发送)"+userName);
		}
		return userName;
	}
	
	
	
	/**
	 * 设置用户头像
	 * 
	 * @param hxUserName
	 */
	public static void setUserAvatar(Context context, String hxUserName,
			ImageView imageView) {
		CoworkerEntity user = getUserInfo(hxUserName);
		String userId =user != null?user.getId():getCCUserIdByHXuserName(hxUserName);
		if (!TextUtils.isEmpty(userId)) {
				String url = UrlTools.getTopImage(userId);
				String userNickName = getUserNickName(hxUserName);
				UserUtils.setLogoRect(url,
									imageView, 
									UserUtils.getBackGroudColorByUserIdHash(userId, AppContext.getInstance()),
									UserUtils.getNickRowName(userNickName), AppContext.getInstance());
		} else {
			imageView.setImageResource(R.drawable.ease_default_avatar);
		}
	}

	/**
	 * 设置用户昵称
	 */
	public static void setUserNick(String username, TextView textView) {
		if (textView != null) {
			textView.setText(getUserNickName(username));
		}
	}

	/**
	 * 根据Message获得 用户的信息nickName等 最后不行就返回登录名称
	 * 
	 * @param message
	 * @param userName
	 * @return
	 */
	public static String getNickNameByMessage(EMMessage message,
			String defaultNickName) {
		// 解析出登录名称
		if (message==null) {
			return defaultNickName;
		}
		String nickName;
		if (message.direct == EMMessage.Direct.RECEIVE) {// 是接收的消息 获得发送的人名
			nickName = message.getStringAttribute(
					Constant.MESSAGE_ATTR_MESSAGE_SEND_NICK, defaultNickName);
		} else {// 是发送的然后 获取接受者的
			nickName = message
					.getStringAttribute(
							Constant.MESSAGE_ATTR_MESSAGE_RECEIVE_NICK,
							defaultNickName);
		}
		return nickName;
	}

	/**
	 * 根据用户名获取 用户的 展示NIckName
	 * 
	 * @param hxusername
	 * @return
	 */
	public static String getUserNickName(String hxusername) {
		CoworkerEntity user = getUserInfo(hxusername);
		if (user == null) {// 当前本地保存的用户中没有
			String userNickName = getCCUserNickNameByHXuserName(hxusername,"同事"+hxusername.hashCode());
			return userNickName;
		}
		return user.getName();
	}
	
	/**
	 * 获得聊天信息的用户名
	 */
	public static String getUserNick(EMMessage message) {
		if (message == null) {
			return "未知同事";
		}
		CoworkerEntity user = getUserInfo(message.getFrom());
		if (user == null) {// 当前本地保存的用户中没有
			String loginName = ContactsManager.getInstance().deCodeHXuserName(
					message.getFrom());
			String nickName = "新同事";
			if (message.direct == EMMessage.Direct.RECEIVE) {
				nickName = message.getStringAttribute(
						Constant.MESSAGE_ATTR_MESSAGE_SEND_NICK, loginName);
			}
			return nickName;
		}
		return user.getName();
	}


}
